#include "drv_usart.h"
void drv_usart6_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用模式
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_14;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOG, & GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOG, GPIO_PinSource9, GPIO_AF_USART6);
	GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_USART6);
	
	USART_InitStruct.USART_BaudRate = 9600;
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_InitStruct.USART_Parity = USART_Parity_No;//
	USART_InitStruct.USART_StopBits = USART_StopBits_1;
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART6, &USART_InitStruct);
	
	USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);//开启接收中断
	USART_ITConfig(USART6, USART_IT_IDLE, ENABLE);//开启空闲中断
	NVIC_InitStruct.NVIC_IRQChannel = USART6_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
	USART_ClearFlag(USART6, USART_FLAG_TC);//清空发送完成标志，否则会出现第一个字母丢失
	USART_Cmd(USART6, ENABLE);
}
int fputc(int ch,FILE *f)//重构fputc函数
{
	USART_SendData(USART6, ch);
	while(USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET);//判断数据是否发送完
	USART_ClearFlag(USART6, USART_FLAG_TC);//Clears the USARTx's pending flags
	return ch;
}
#include "drv_led.h"
#include "drv_beep.h"
#include "drv_water.h"
#include <stdio.h>
#include <string.h>
#define CMD_LED_ON    "led on"
#define CMD_LED_OFF   "led off"
#define CMD_BEEP_ON   "beep on"
#define CMD_BEEP_OFF  "beep off"

uint8_t cmd = 0;   
uint8_t arr[64];      
uint16_t i = 0;     
uint8_t c = 0;   
uint8_t data;
void USART6_IRQHandler(void)
{
	if(USART_GetITStatus(USART6, USART_IT_RXNE) != RESET)
	{
		char data6;
		data6 = USART_ReceiveData(USART6);  
		USART_ClearITPendingBit(USART6, USART_IT_RXNE);
		//USART_SendData(USART6, USART_ReceiveData(USART6));
		 printf("111\r\n" );     
        USART_SendData(USART6, data);    
        if (data != '\r' && data != '\n') 
        {
            arr[i++] = data;
        } 
        else if (data == '\n') 
        {
            arr[i] = '\0'; // 添加字符串结束符
            c = 1;
            i = 0;
        }
		
        if(USART_GetFlagStatus(USART6, USART_FLAG_IDLE))
        {
            USART_ClearITPendingBit(USART6, USART_IT_IDLE);
            USART_ReceiveData(USART6); 
            		                
        }
    }
}

void parse_command(const char* cmd) 
{      
    if (strcmp(cmd, CMD_LED_ON) == 0) 
    {
        drv_water1_open();
    } 
    else if (strcmp(cmd, CMD_LED_OFF) == 0) 
    {
        drv_water1_close();
    } 
    else if (strcmp(cmd, CMD_BEEP_ON) == 0) 
    {
        drv_beep_open();
    } 
    else if (strcmp(cmd, CMD_BEEP_OFF) == 0) 
    {
        drv_beep_close();
    }
}
